﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using HANS.DataBaseTool;
using HANS_Base;
using HANS_BackStage.Model.DbModel;
using HANS.Utility.Extensions;

namespace HANS_BackStage.Impl
{
    public class Role_PermiServices
    {
        private static HANS_BackStage.DAL.BackStageDataEntityDAL<Backstage_role> roleDal = new DAL.BackStageDataEntityDAL<Backstage_role>();
        private static HANS_BackStage.DAL.BackStageDataEntityDAL<Backstage_permission> permissionDal = new DAL.BackStageDataEntityDAL<Backstage_permission>();
        /// <summary>
        /// 根据id获取角色实体
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public static Backstage_role GetRole(string roleId) {
            Backstage_role role = new Backstage_role();
            role = roleDal.Load(roleId);
            return role;
        }

        /// <summary>
        /// 添加某个角色的权限
        /// </summary>
        /// <param name="id"></param>
        /// <param name="permissionList"></param>
        /// <returns></returns>
        public static Msg InsertRolePermmission(string id, List<Backstage_permission> permissionList)
        {
            Msg msg = new Msg();
            string guid = "";
            List<string> sqlList = new List<string>();
            foreach (var item in permissionList)
            {

                string sqlStr = "INSERT INTO ROLE_PERMISSION VALUES ('{0}', '{1}', '{2}', null)";
                sqlList.Add(string.Format(sqlStr, guid.NewGuid(), id, item.Id));
            }

            DbHelper.TranscationExecute(sqlList, HANSDataBaseLink.admin,msg);
            return msg;
        }

        /// <summary>
        /// 删除某个角色下的所有权限
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public static Msg DelteRolePermissionByRoleId(string roleid) {

            Msg msg = new Msg();
            string errmsg = "";
            string sqlStr = "DELETE FROM role_permission WHERE role_permission.RoleID='{0}'";
            DbHelper.ExecuteSQLHelper(string.Format(sqlStr, roleid), HANSDataBaseLink.admin, ref errmsg);
            msg.AddMsg(errmsg);
            return msg;
        }


        /// <summary>
        /// 根据id获取权限实体
        /// </summary>
        /// <param name="permissionId"></param>
        /// <returns></returns>
        public static Backstage_permission GetPermission(string permissionId) {
            Backstage_permission permission = new Backstage_permission();
            permission = permissionDal.Load(permissionId);
            return permission;
        }
        /// <summary>
        /// 根据ID获取角色List_string
        /// </summary>
        /// <param name="Id">admin_id</param>
        /// <returns></returns>
        public static List<string> GetRoles(string Id) {
            string msg = "";
            List<string> Roles = new List<string>();
            string sqlStr = "SELECT RoleID FROM admin_role  WHERE  AdminId='{0}'";
            DataSet dsRoles = DbHelper.SqlHelper(string.Format(sqlStr, Id), HANS_Base.HANSDataBaseLink.admin, ref msg);
            if (dsRoles != null && dsRoles.Tables[0] != null && dsRoles.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in dsRoles.Tables[0].Rows)
                {
                    Roles.Add(item["RoleID"].ToString());
                }

            }
            return Roles;
        }

        /// <summary>
        /// 根据角色id 获取权限List_string
        /// </summary>
        /// <param name="Id">Peimission_Id</param>
        /// <returns></returns>
        public static List<string> GetPermissions(string Id)
        {
            string msg = "";
            List<string> Peimissions = new List<string>();
            string sqlStr = "SELECT PermissionID FROM role_permission  WHERE  RoleID ='{0}'";
            DataSet dsPeimissions = DbHelper.SqlHelper(string.Format(sqlStr, Id), HANS_Base.HANSDataBaseLink.admin, ref msg);
            if (dsPeimissions != null && dsPeimissions.Tables[0] != null && dsPeimissions.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in dsPeimissions.Tables[0].Rows)
                {
                    Peimissions.Add(item["PermissionID"].ToString());
                }

            }
            return Peimissions;
        }

        /// <summary>a
        /// 根据idList 获取角色List
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static List<Backstage_role> GetRoles(List<string> idList) {
            List<Backstage_role> roleList = new List<Backstage_role>();
            foreach (var item in idList)
            {
                roleList.Add(GetRole(item));
            }
            return roleList;
        }

        /// <summary>
        /// 根据id List获取权限List
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static List<Backstage_permission> GetPermissions(List<string> idList) {
            List<Backstage_permission> permissionList=new List<Backstage_permission> ();
            foreach (var item in idList)
            {
                permissionList.Add(GetPermission(item));
            }

            return permissionList;
        }

        /// <summary>
        /// 根据权限id获取孩子List
        /// </summary>
        /// <param name="permission_id"></param>
        /// <returns></returns>
        public  static List<Backstage_permission> GetPerChildrenList(string permission_id) {
            string msg = "";
            List<Backstage_permission> childrenList = new List<Backstage_permission>();
            string sqlStr = "SELECT * from backstage_permission WHERE ParentID='{0}' ORDER BY  `Ordernum`";
            DataSet dsChildrens = DbHelper.SqlHelper(string.Format(sqlStr, permission_id), HANS_Base.HANSDataBaseLink.admin, ref msg);
            if (dsChildrens != null && dsChildrens.Tables[0] != null && dsChildrens.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in dsChildrens.Tables[0].Rows)
                {
                   
                    Backstage_permission permission = new Backstage_permission();
                    permission = permissionDal.CreateEntityByDataRow(item);
                    childrenList.Add(permission);
                }
            }

            return childrenList;
        }

        /// <summary>
        /// 取所有的权限
        /// </summary>
        /// <returns></returns>
        public static List<Backstage_permission> GetAllPermissions() {
            return permissionDal.List();
        }

        /// <summary>
        /// 获取某个管理员的角色列表
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns></returns>
        public static List<Backstage_role> GetRoleSelf(string adminId) {
            string msg = "";
            string sqlStr = "SELECT	* FROM backstage_role WHERE ID IN (SELECT	RoleID	FROM	admin_role	WHERE AdminID = '{0}')";
            List<Backstage_role> Rolelist = new List<Backstage_role>();
            DataSet dsRoles = DbHelper.SqlHelper(string.Format(sqlStr, adminId), HANS_Base.HANSDataBaseLink.admin, ref msg);
            if (dsRoles != null && dsRoles.Tables[0] != null && dsRoles.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in dsRoles.Tables[0].Rows)
                {
                    Rolelist.Add(roleDal.CreateEntityByDataRow(item));
                }
            }
            return Rolelist;
        }

        /// <summary>
        /// 获取角色的权限
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public static List<Backstage_permission> GetPerminssionByRoleId(string roleid) {
            return Role_PermiServices.GetPermissions(Role_PermiServices.GetPermissions(roleid));
        }

        #region 角色/管理员管理
        /// <summary>
        /// 添加某个管理员的角色
        /// </summary>
        /// <param name="adminid"></param>
        /// <param name="roles"></param>
        /// <returns></returns>
        public static Msg SetRole_Admin(string adminid, string[] roles) {
            Msg msg = new Msg();
            List<string> sqlStrList = new List<string>();
            foreach (var item in roles)
            {
                string guid = string.Empty;
                string sqlStr = "INSERT INTO admin_role  VALUES('{0}','{1}','{2}',NULL)";
                sqlStrList.Add(string.Format(sqlStr, guid.NewGuid(),adminid,item));
            }

            DbHelper.TranscationExecute(sqlStrList, HANSDataBaseLink.admin, msg);
            return msg;
        }

        /// <summary>
        /// 删除某个管理员的所有角色
        /// </summary>
        /// <param name="adminid"></param>
        /// <returns></returns>
        public static Msg DeleteRole_Admin(string  adminid) {
            Msg msg = new Msg();
            int Result = 0;
            string sqlStr = "DELETE FROM admin_role WHERE AdminID='{0}'";
            Result = DbHelper.ExecuteSQLHelper(string.Format(sqlStr, adminid), HANSDataBaseLink.admin,msg);
            return msg;
        }


        /// <summary>
        /// 删除某个管理员的某个角色
        /// </summary>
        /// <param name="adminid"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public static Msg DeleteRole_Admin(string adminid, string roleid) {
            Msg msg = new Msg();
            int Result = 0;
            string sqlStr = "DELETE FROM admin_role WHERE admin_role.AdminID='{0}' AND admin_role.RoleID='{1}'";
            Result = DbHelper.ExecuteSQLHelper(string.Format(sqlStr, adminid,roleid), HANSDataBaseLink.admin, msg);
            return msg;
        }


        /// <summary>
        /// 根据角色id获取管理员id——list
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public static List<string> GetAdminStrByRoleId(string roleid) {
            string msg = "";
            List<string> Admins = new List<string>();
            string sqlStr = "SELECT AdminID FROM admin_role  WHERE  RoleID='{0}'";
            DataSet dsAdmins = DbHelper.SqlHelper(string.Format(sqlStr, roleid), HANS_Base.HANSDataBaseLink.admin, ref msg);
            if (dsAdmins != null && dsAdmins.Tables[0] != null && dsAdmins.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in dsAdmins.Tables[0].Rows)
                {
                    Admins.Add(item["AdminID"].ToString());
                }

            }
            return Admins;
        }

        
        #endregion
    }
}
